// 操作日志数据层

package dao

import (
	"admin-go-api/api/entity"
	"admin-go-api/pkg/db"
)

// CreateSysOperationLog 新增操作日志
func CreateSysOperationLog(log entity.SysOperationLog) {
	db.Db.Create(&log)
}

// GetSysOperationLog 分页查询操作日志
func GetSysOperationLog(PageSize, PageNum int,
	UserName, BeginTime, EndTime string) (sysOperationLog []entity.SysOperationLog, count int64) {
	curDb := db.Db.Table("sys_operation_log")
	if UserName != "" {
		curDb = curDb.Where("username = ?", UserName)
	}
	if BeginTime != "" && EndTime != "" {
		curDb = curDb.Where("create_time BETWEEN ? AND ?", BeginTime, EndTime)
	}
	curDb.Count(&count)
	curDb.Limit(PageSize).Offset((PageNum - 1) * PageSize).Order("create_time DESC").Find(&sysOperationLog)
	return sysOperationLog, count
}

// DeleteOperationLogById 根据id删除操作日志
func DeleteOperationLogById(dto entity.SysOperationLogIdDto) {
	db.Db.Where("id = ?", dto.Id).Delete(&entity.SysOperationLog{})
}

// BatchDeleteSysOperationLog 批量删除操作日志
func BatchDeleteSysOperationLog(dto entity.DelSysOperationLogDto) {
	db.Db.Where("id in (?)", dto.Ids).Delete(&entity.SysOperationLog{})
}

// CleanSysOperationLog 清空操作日志
func CleanSysOperationLog() {
	db.Db.Exec("truncate table sys_operation_log")
}
